<?php
namespace App\Http\Controllers\Admin;

use App\Service\Admin\IndexService;
use Illuminate\Http\Request;

class IndexController extends BaseController
{
    //首页

    public function __construct()
    {
    }

    //登录
    public function dologin(Request $request)
    {
        $uname = $request->post('uname', '');
        $upwd = $request->post('upwd', '');
        if (mb_strlen($uname) < 2 || mb_strlen($uname) > 255) {
            return $this->toJson(-2, '账号名必须2到255位字符之间');
        }
        if (strlen($upwd) < 6 || strlen($upwd) > 20) {
            return $this->toJson(-2, '密码必须6到20位字符之间');
        }
        if (!preg_match('/[0-9a-zA-Z*#&_]{6,20}/', $upwd)) {
            return $this->toJson(-2, '密码格式错误');
        }
        $res = (new IndexService())->dologin($uname, $upwd);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //获取左侧菜单
    public function leftMenu(Request $request)
    {
        $aid = $request->post('aid', 0);
        if (!$aid) {
            return $this->toJson(-2, '没有登录');
        }
        $res = (new IndexService())->getLeftMenu();
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //列表
    public function getActionIndex(Request $request)
    {
//        $pages['start'] = $request->post('start', 1);
//        $pages['limit'] = $request->post('limit', 10);
        $res = (new IndexService())->getActionIndex();
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //一个
    public function getActionOne(Request $request)
    {
        $id = $request->post('id');
        $res = (new IndexService())->getActionOne($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //父级
    public function getParent()
    {
        $res = (new IndexService())->getParent();
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //保存
    public function actionUpdate(Request $request)
    {
        $id = $request->post('id');
        $pas['title'] = $request->post('title');
        $pas['pid'] = $request->post('pid', 0);
        $pas['link'] = $request->post('link');
        if (!$pas['title']) {
            return $this->toJson(-2, '功能名称必须的');
        }
        $res = (new IndexService())->actionUpdate($id, $pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //删除
    public function actionDel(Request $request)
    {
        $id = $request->post('id', 0);
        $del = $request->post('del', 0);
        $res = (new IndexService())->actionDel($id, $del);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }
}